package initialize

import (
	"fmt"
	"github.com/spf13/viper"
	"goods_srv/global"
	"goods_srv/models"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func init() {
	Viper()
	Nacos()
	Mysql()
}
func Viper() {
	viper.SetConfigFile("./config.dev.yaml")
	err := viper.ReadInConfig()
	if err != nil {
		panic(err)
	}
	viper.Unmarshal(&global.NacosConfig)
	fmt.Println("nacos配置读取", global.NacosConfig)
}
func Mysql() {
	var err error
	config := global.Config.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&parseTime=True", config.User, config.Password, config.Host, config.Port, config.Database)
	global.DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		panic(err)
	}
	fmt.Println("mysql连接")
	global.DB.AutoMigrate(&models.Category{}, &models.Product{}, &models.ProductSku{}, &models.Spec{}, &models.SpecValue{}, &models.ProductSpec{})
}
